package conf

import (
	"fmt"
	"strconv"

	"gopkg.in/ini.v1"
)

var (
	Debug       bool
	HttpAddress string
	HttpPort    string
	Db          string
	DbHost      string
	DbPort      string
	DbUser      string
	DbPassWord  string
	DbName      string
	RedisDb     string
	RedisAddr   string
	RedisPwd    string
	RedisDbName int64
)

func Init() {
	file, err := ini.Load("conf/config.ini")
	if err != nil {
		fmt.Println("配置文件读取错误")
	}
	loadService(file)
	loadRedis(file)
	loadMysql(file)
}
func loadService(file *ini.File) {
	debug, err := strconv.ParseBool(file.Section("service").Key("Debug").String())
	if err != nil {
		fmt.Println("Debug 初始化失败")
	}
	Debug = debug
	HttpAddress = file.Section("service").Key("HttpAddress").String()
	HttpPort = file.Section("service").Key("HttpPort").String()
}

func loadRedis(file *ini.File) {
	RedisDb = file.Section("redis").Key("RedisDb").String()
	RedisAddr = file.Section("redis").Key("RedisAddr").String()
	RedisPwd = file.Section("redis").Key("RedisPwd").String()
	redisDbName, err := strconv.ParseInt(file.Section("redis").Key("RedisDbName").String(), 12, 64)
	if err != nil {
		fmt.Println("RedisDbName 初始化失败")
	}
	RedisDbName = redisDbName
}

func loadMysql(file *ini.File) {
	Db = file.Section("mysql").Key("Db").String()
	DbHost = file.Section("mysql").Key("DbHost").String()
	DbPort = file.Section("mysql").Key("DbPort").String()
	DbUser = file.Section("mysql").Key("DbUser").String()
	DbPassWord = file.Section("mysql").Key("DbPassWord").String()
	DbName = file.Section("mysql").Key("DbName").String()
}
